﻿#coding=utf-8
from django.db import models
from django.core.exceptions import ObjectDoesNotExist
#from django.db import connection
#from phonesite.down.views import *

# Create your models here.
class Player(models.Model):
    #username只可以设一次，以后不可更改。这是为了避免别人联接到你的主页时产生坏链接。如果现在你不是百分之百的确定，可以暂时留     #为空，等以后再说。
    #username最长15个字符，可以包含英文字母、数字、和三个符号(. - _)。第一个字符必须为英文字母。大小写有区分，比如datou和         #DaTou是不同的两个成员。 
    username = models.CharField(maxlength=15,blank=False)

    #登陆密码，可修改
    password = models.CharField(maxlength=10,blank=False)

    #昵称，可修改
    nickname = models.CharField(maxlength=10,null=True,blank=True)

    #Email，可修改，
    email = models.EmailField(blank=False)

    #手机型号，可修改
    phone = models.CharField(maxlength=15,null=True,blank=True)

    #注册日期
    datetime = models.DateTimeField(auto_now_add = True)

    #个人头像，可修改,上传到'icons'目录下
    pic = models.ImageField(upload_to='icons')

    #自我介绍，可修改
    intro = models.TextField(null=True,blank=True)

    

    #是否给我发电子邮件
    isReceiveEmail = models.BooleanField(null=True,blank=True)
    
    def __str__(self):
        return self.username

    class Admin:
        pass

#############################################
#
#下载频道的所有model
#date:2008,1,25
#
#############################################

TYPE_SOFT_GAME_CHOICES = (
    ('JAR', 'JAR'),
    ('SIS', 'SIS'),
    ('SISX', 'SISX'),
    ('RAR', 'RAR'),
    ('ZIP', 'ZIP'),
)

OS_SOFT_CHOICES = (
    ('诺基亚S60平台', '诺基亚S60平台'),
    ('诺基亚S60第三版平台', '诺基亚S60第三版平台'),
    ('Linux智能手机平台', 'Linux智能手机平台'),
    ('UIQ智能手机平台', 'UIQ智能手机平台'),
    ('Pocket PC平台', 'Pocket PC平台'),
    ('SmartPhone平台', 'SmartPhone平台'),
)

OS_SOFT_LANGS = (
    ('中文', '中文'),
    ('英文', '英文'),
    ('简体中文', '简体中文'),
)

OS_SOFT_RANKS = (
    ('1', '1'),
    ('2', '2'),
    ('3', '3'),
    ('4', '4'),
    ('5', '5'),
)

OS_GAME_SERIES = (
    ('7210系列', '1'),
    ('6101系列', '2'),
    ('6230i系列', '3'),
    ('7260系列', '4'),
    ('7370系列', '5'),
    ('QD系列', '1'),
    ('7610系列', '2'),
    ('3250系列', '3'),
    ('N73系列', '4'),
    ('N80系列', '5'),
    
)

OS_GAME_PHONE = (
    ('2650', '1'),
    ('3100', '2'),
    ('3108', '3'),
    ('3120', '4'),
    ('3200', '5'),
    ('3300', '1'),
    ('3510', '2'),
    ('3530', '3'),
    ('3560', '4'),
    ('3610', '5'),
    
)

class Wap(models.Model):

    global_id = models.IntegerField(blank=False)
    
    def __str__(self):
        return str(self.global_id)

    class Admin:
        pass

class SoftCate(models.Model):
    cate_name = models.CharField(maxlength=10,blank=False)
    
    def __str__(self):
        return self.cate_name

    ###@models.permalink 这个@models.permalink是什么意思？？
    def get_absolute_url(self):
        return "/soft/%i/" % self.id

    class Admin:
        pass

class NewManager(models.Manager):
    def get_query_set(self):
        return super(NewManager, self).get_query_set().order_by('-datetime')

class HotManager(models.Manager):
    def get_query_set(self):
        return super(HotManager, self).get_query_set().order_by('-downcount')



class Soft(models.Model):

    #软件名称
    name = models.CharField(maxlength=50,blank=False)

    #上传日期
    datetime = models.DateTimeField(auto_now_add = True)

    #上传人
    uploader = models.ForeignKey(Player)
    
    #软件大小
    size = models.CharField(maxlength=20,blank=False)

    #软件分类
    cate = models.ForeignKey(SoftCate)

    #软件截图，上传到'pics/softs/'
    pic = models.ImageField(upload_to='pic/soft')

    #软件介绍
    intro = models.TextField(null=True,blank=True)

    #软件评价
    rank = models.CharField(maxlength=20,choices=OS_SOFT_RANKS,blank=False)

    #语言
    lang = models.CharField(maxlength=20,choices=OS_SOFT_LANGS,blank=False)

    #是否推荐
    isRecommend = models.BooleanField(null=True,blank=True)

    #软件格式：sis,jar,sisx等，具体参考TYPE_SOFT_GAME_CHOICES
    format = models.CharField(maxlength=10,choices=TYPE_SOFT_GAME_CHOICES,blank=False)

    #软件运行OS：S60,S90,S60 3rd,Java平台，Windows Mobile等，具体参考OS_SOFT_CHOICES
    os = models.CharField(maxlength=100,choices=OS_SOFT_CHOICES,blank=False)

    #WAP下载编号
    wapid = models.IntegerField(blank=False)

    #软件下载地址
    downaddr = models.FileField(upload_to='file/soft')

    #软件下载次数
    downcount = models.CharField(maxlength=20,blank=False)

    #定义三个Manager
    objects = models.Manager()
    news = NewManager()
    hots = HotManager()

    #override this save() method for 插入wapid
    def save(self):
        if self.id:
            pass
        else:
            try:
              wap = Wap.objects.get(id=1)
            except ObjectDoesNotExist:
              wap = Wap(id=1, global_id=0)
              #wap.save()
            wap.global_id = wap.global_id+1
            self.wapid = wap.global_id
            wap.save()
        super(Soft, self).save()

    ###@models.permalink 这个@models.permalink是什么意思？？
    def get_absolute_url(self):
        return "/soft/detail/%i/" % self.id
    
    def get_down_url(self):
        return "/down/soft/%i/" % self.id
   
 
    def __str__(self):
        return self.name

    class Admin:
        pass

class SoftReply(models.Model):

    soft = models.ForeignKey(Soft)

    content = models.TextField(blank=False)

    author = models.ForeignKey(Player)

    datetime = models.DateTimeField(auto_now_add = True)
    
    
    def __str__(self):
        return self.content

    class Admin:
        pass




class Phone(models.Model):
    phone_name = models.CharField(maxlength=50,blank=False)

    count = models.CharField(maxlength=50,blank=False)

    factory = models.CharField(maxlength=50,blank=False)
    
    def __str__(self):
        return self.phone_name

    class Admin:
        pass



class GameCate(models.Model):
    cate_name = models.CharField(maxlength=10,blank=False)
    
    def __str__(self):
        return self.cate_name

     ###@models.permalink 这个@models.permalink是什么意思？？
    def get_absolute_url(self):
        return "/game/%i/" % self.id

    class Admin:
        pass


class Game(models.Model):

    #游戏名称
    name = models.CharField(maxlength=50,blank=False)

    #上传日期
    datetime = models.DateTimeField(auto_now_add = True)

    #上传人
    uploader = models.ForeignKey(Player)
    
    #游戏大小
    size = models.CharField(maxlength=20,blank=False)

    #游戏分类
    cate = models.ForeignKey(GameCate)

    #游戏截图
    pic = models.ImageField(upload_to='pic/game')

    #游戏介绍
    intro = models.TextField(null=True,blank=True)

    #游戏评价
    rank = models.CharField(maxlength=20,choices=OS_SOFT_RANKS,blank=False)

    #语言
    lang = models.CharField(maxlength=20,choices=OS_SOFT_LANGS,blank=False)

    #是否推荐
    isRecommend = models.BooleanField(null=True,blank=True)

    #游戏格式
    format = models.CharField(maxlength=10,choices=TYPE_SOFT_GAME_CHOICES,blank=False)

    #游戏适用机型
    os = models.CharField(maxlength=2000,blank=False)

    #WAP下载编号
    wapid = models.IntegerField(blank=False)

    #游戏下载地址
    downaddr = models.FileField(upload_to='file/game')

    #游戏下载次数
    downcount = models.CharField(maxlength=20,blank=False)

    #定义三个Manager
    objects = models.Manager()
    news = NewManager()
    hots = HotManager()


    #override this save() method for 插入wapid
    def save(self):
        if self.id:
            pass
        else:
            try:
              wap = Wap.objects.get(id=1)
            except ObjectDoesNotExist:
              wap = Wap(id=1, global_id=0)
              #wap.save()
            wap.global_id = wap.global_id+1
            self.wapid = wap.global_id
            wap.save()
        super(Game, self).save()
    
    ###@models.permalink 这个@models.permalink是什么意思？？
    def get_absolute_url(self):
        return "/game/detail/%i/" % self.id

    def get_down_url(self):
        return "/down/game/%i/" % self.id


    def __str__(self):
        return self.name

    class Admin:
        pass

class GameReply(models.Model):

    game = models.ForeignKey(Game)

    content = models.TextField(blank=False)

    author = models.ForeignKey(Player)

    datetime = models.DateTimeField(auto_now_add = True)
    
    
    def __str__(self):
        return self.content

    class Admin:
        pass

class BookCate(models.Model):
    cate_name = models.CharField(maxlength=10,blank=False)
    
    def __str__(self):
        return self.cate_name

     ###@models.permalink 这个@models.permalink是什么意思？？
    def get_absolute_url(self):
        return "/book/%i/" % self.id

    class Admin:
        pass

TYPE_BOOK_CHOICES = (
    ('TXT', 'TXT'),
    ('JAR', 'JAR'),
    ('UMD', 'UMD'),
    ('SR', 'Senior'),
    ('GR', 'Graduate'),
)

class Book(models.Model):

    #图书名称
    name = models.CharField(maxlength=50,blank=False)

    #上传日期
    datetime = models.DateTimeField(auto_now_add = True)

    #上传人
    uploader = models.ForeignKey(Player)
    
    #图书大小
    size = models.CharField(maxlength=20,blank=False)

    #图书分类
    cate = models.ForeignKey(BookCate)

    #图书格式
    format = models.CharField(maxlength=10,choices=TYPE_BOOK_CHOICES,blank=False)

    #图书截图
    pic = models.ImageField(upload_to='pic/book')

    #图书介绍
    intro = models.TextField(null=True,blank=True)

    #图书评价
    rank = models.CharField(maxlength=20,choices=OS_SOFT_RANKS,blank=False)

    #语言
    lang = models.CharField(maxlength=20,choices=OS_SOFT_LANGS,blank=False)

    #是否推荐
    isRecommend = models.BooleanField(null=True,blank=True)

    #图书作者
    author = models.CharField(maxlength=50,null=True,blank=True)

    #WAP下载编号
    wapid = models.IntegerField(blank=False)

    #图书下载地址
    downaddr = models.FileField(upload_to='file/book')

    #图书下载次数
    downcount = models.CharField(maxlength=20,blank=False)

    #定义三个Manager
    objects = models.Manager()
    news = NewManager()
    hots = HotManager()


    #override this save() method for 插入wapid
    def save(self):
        if self.id:
            pass
        else:
            try:
              wap = Wap.objects.get(id=1)
            except ObjectDoesNotExist:
              wap = Wap(id=1, global_id=0)
              #wap.save()
            wap.global_id = wap.global_id+1
            self.wapid = wap.global_id
            wap.save()
        super(Book, self).save()


    ###@models.permalink 这个@models.permalink是什么意思？？
    def get_absolute_url(self):
        return "/book/detail/%i/" % self.id

    def get_down_url(self):
        return "/down/book/%i/" % self.id

    def __str__(self):
        return self.name

    class Admin:
        pass

class BookReply(models.Model):

    book = models.ForeignKey(Book)

    content = models.TextField(blank=False)

    author = models.ForeignKey(Player)

    datetime = models.DateTimeField(auto_now_add = True)
    
    
    def __str__(self):
        return self.content

    class Admin:
        pass


#友情链接
class FriendLink(models.Model):

    name = models.CharField(maxlength=20,blank=False)

    link = models.CharField(maxlength=100,blank=False)

    count = models.IntegerField(blank=False)

    datetime = models.DateTimeField(auto_now_add = True)
    
    
    def __str__(self):
        return self.name

    class Admin:
        pass
##########################################################
#
#玩家乐园model
#
#
##########################################################


